# Define paths to the target files
$filesToModify = @("src\entity\mod.rs", "src\entity\prelude.rs")

# Run the sea-orm-cli command to regenerate entities
Write-Host "Running sea-orm-cli to generate entities..."
sea-orm-cli generate entity -o src/entity

# Check if the sea-orm-cli command succeeded
if ($LASTEXITCODE -ne 0) {
    Write-Host "Error: sea-orm-cli command failed."
    exit $LASTEXITCODE
}

# Function to update a file with #![allow(unused)]
function Add-AllowUnused {
    param (
        [string]$FilePath
    )

    if (Test-Path $FilePath) {
        $lines = Get-Content $FilePath

        # Only proceed if the attribute is not already present
        if (-not ($lines -contains "#![allow(unused)]")) {
            if ($lines.Count -gt 0 -and $lines[0] -match "^\/\/!") {
                # Insert after the first comment line
                $lines = $lines[0] + "`r`n" + "#![allow(unused)]" + "`r`n" + ($lines[1..($lines.Count - 1)] -join "`r`n")
            } else {
                # Insert at the top
                $lines = "#![allow(unused)]" + "`r`n" + ($lines -join "`r`n")
            }

            Set-Content -Path $FilePath -Value $lines
            Write-Host "Updated $FilePath successfully."
        } else {
            Write-Host "$FilePath already contains #![allow(unused)]"
        }
    } else {
        Write-Host "$FilePath not found."
    }
}

# Apply the function to each target file
foreach ($file in $filesToModify) {
    Add-AllowUnused -FilePath $file
}